using System.Collections.Generic;
using System.Linq;
using TeachingManagementSystem.DAL;
using TeachingManagementSystem.Models;
using System;

namespace TeachingManagementSystem.BLL
{
    public class ScheduleBLL
    {
        private DataContext _context;

        public ScheduleBLL()
        {
            _context = new DataContext();
        }

        // 获取所有课表
        public List<Schedule> GetAllSchedules()
        {
            return _context.Schedules.ToList();
        }

        // 根据ID获取课表
        public Schedule GetScheduleByID(int scheduleID)
        {
            return _context.Schedules.FirstOrDefault(s => s.ScheduleID == scheduleID);
        }

        // 根据教师ID获取课表
        public List<Schedule> GetSchedulesByTeacherID(int teacherID)
        {
            return _context.Schedules.Where(s => s.TeacherID == teacherID).ToList();
        }

        // 根据课程ID获取课表
        public List<Schedule> GetSchedulesByCourseID(int courseID)
        {
            return _context.Schedules.Where(s => s.CourseID == courseID).ToList();
        }

        // 根据教室ID获取课表
        public List<Schedule> GetSchedulesByClassroomID(int classroomID)
        {
            return _context.Schedules.Where(s => s.ClassroomID == classroomID).ToList();
        }

        // 添加课表
        public void AddSchedule(Schedule schedule)
        {
            // 手动获取最大ID并设置递增ID
            int maxId = _context.Schedules.Any() ? _context.Schedules.Max(s => s.ScheduleID) : 0;
            schedule.ScheduleID = maxId + 1;
            _context.Schedules.Add(schedule);
            _context.SaveChanges(); // 保存到数据库
        }

        // 更新课表
        public void UpdateSchedule(Schedule schedule)
        {
            var existingSchedule = _context.Schedules.FirstOrDefault(s => s.ScheduleID == schedule.ScheduleID);
            if (existingSchedule != null)
            {
                existingSchedule.CourseID = schedule.CourseID;
                existingSchedule.ClassroomID = schedule.ClassroomID;
                existingSchedule.TeacherID = schedule.TeacherID;
                existingSchedule.DayOfWeek = schedule.DayOfWeek;
                existingSchedule.StartSection = schedule.StartSection;
                existingSchedule.SectionCount = schedule.SectionCount;
                existingSchedule.Semester = schedule.Semester;
                
                _context.SaveChanges(); // 保存到数据库
            }
        }

        // 删除课表
        public void DeleteSchedule(int scheduleID)
        {
            var schedule = _context.Schedules.FirstOrDefault(s => s.ScheduleID == scheduleID);
            if (schedule != null)
            {
                _context.Schedules.Remove(schedule);
                _context.SaveChanges(); // 保存到数据库
            }
        }

        // 检查时间冲突
        public bool CheckTimeConflict(int scheduleID, int classroomID, DayOfWeek dayOfWeek, int startSection, int sectionCount)
        {
            if (_context.Schedules == null) return false;
            
            // 计算结束节次
            int endSection = startSection + sectionCount - 1;
            
            // 检查是否与现有课表冲突
            return _context.Schedules.Any(s => 
                s.ClassroomID == classroomID && 
                s.DayOfWeek == dayOfWeek && 
                s.ScheduleID != scheduleID &&
                // 时间重叠检查
                !(s.StartSection > endSection || s.StartSection + s.SectionCount - 1 < startSection));
        }

        // 获取特定学期的课表
        public List<Schedule> GetSchedulesBySemester(string semester)
        {
            if (string.IsNullOrEmpty(semester))
                return new List<Schedule>();
                
            return _context.Schedules?.Where(s => s.Semester == semester).ToList() ?? new List<Schedule>();
        }
        

    }
}